from django import forms
from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import render_to_response
import metroscope.settings as settings
import metroscope.codewiki.models as models
import os
import re
import datetime
import runscrapers

# event fields include
# "title", "summary", "url", "source", "refid", "quantity", "evt_time", "postcode", "lat", "lon", "district", "radius", "sumbitter", "sub_time"



def scopeapi1(request, param1, param2, param3):
    scopeevents = models.ScopeEvent.objects.filter(source="missing Cat")
    message = ""
    
    monthcats = [ []  for i in range(12) ]
    for scopeevent in scopeevents:
        monthcats[scopeevent.evt_time.month - 1].append(scopeevent)
    
    return render_to_response('scopeapi1.html', {'message':message, 'monthcats':monthcats, 'settings': settings})

def scopeapi2(request, param1, param2, param3):
    return render_to_response('scopeapi1.html', {'message':str((param1, param2, param3)), 'settings': settings})

def scopemappingapi(request, param):
    events = models.ScopeEvent.objects.filter(source="nofloodwarning")[:100]
    users = models.ScopeUser.objects.filter(name="sym")
    message = "The parameter string is: '%s'" % param
    #message = str([u.password for u in users]) + "hihi"
    return render_to_response('scopemappingapi.html', {'message':message, 'events':events, 'settings': settings})

def observer(request, observername, tail):
    exename = os.path.join(settings.CODEWIKI_DIR, "observers", observername + ".py")
    ptail = tail and (" --tail " + tail) or ""
    pqs = request.META["QUERY_STRING"] and (" --query " + request.META["QUERY_STRING"]) or ""
    sparam = "render" + ptail + pqs
    content = runscrapers.RunFileA(exename, sparam) 
    return HttpResponse(content=content)

